package com.rock.code.common.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author Marka
 * @date 2015/12/24
 * @Time 10:42
 * @Version 1.0
 */

public class ErrorLogUtil {
    Log logger = LogFactory.getLog(this.getClass());
    public void writeLog(Exception e){
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setCharacterEncoding("UTF-8");   //一般国内浏览器设置的编码
        PrintWriter out = null;
        try {
            out = response.getWriter();
            logger.error("\n" + "<=======================================>PRINT SERVICE_ERRO_INFO BEGIN <=======================================>"+"\n");
            if(e.getCause()!=null && (e.getCause()) instanceof CustomException){
                //如果是ajax请求，将业务异常返回给回调函数
                out.write("info:" + ((CustomException)e.getCause()).getErrorInfo());

                //如果是sql异常，把sql异常信息也附件上去显示在页面
                if((Exception)(((CustomException)e.getCause()).getCause()) instanceof Exception){
                    String msg=((Exception)(((CustomException)e.getCause()).getCause())).getMessage();
                    if(msg.indexOf("java.sql.SQLException:")!=-1){
                        if(msg.indexOf("java.sql.SQLException: ORA-00001: unique constraint")!=-1){
                            out.write(";所填信息有字段违反数据库表唯一性约束。");
                        }
                        out.write("\n"+msg.substring(msg.indexOf("java.sql.SQLException:")));

                    }
                }


                //将异常信息写入日志
                logger.error("\n" + "程序运行产生业务异常==================>>>:::"+((CustomException)e.getCause()).getErrorInfo(),e);
            }else{
                out.write("info:" + e);
                logger.error("\n" + "程序运行产生异常==================>>>:::",e);
            }
            logger.error("\n" + "<=======================================>PRINT SERVICE_ERRO_INFO END  <=======================================>" + "\n");
        } catch (IOException e1) {
            e1.printStackTrace();
            logger.error("异常拦截发生错误======》》》：" + e1);
        }catch (RuntimeException e1) {
            e.printStackTrace();
            logger.error("异常拦截发生错误======》》》：" + e1);
        }finally{
            if(out!=null){
                out.close();
            }
        }
    }

    public void writeActionLog(Exception e){
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setCharacterEncoding("UTF-8");   //一般国内浏览器设置的编码
        PrintWriter out = null;
        try {
            out = response.getWriter();
            logger.error("\n" + "<=======================================>PRINT ACTION_ERRO_INFO BEGIN <=======================================>"+"\n");
            if(e.getCause()!=null && (e.getCause()) instanceof CustomException){
                //如果是ajax请求，将业务异常返回给回调函数
                out.write("info:" + ((CustomException)e.getCause()).getErrorInfo());
                //将异常信息写入日志
                logger.error("\n" + "程序运行产生业务异常==================>>>:::"+((CustomException)e.getCause()).getErrorInfo(),e);
            }else{
                out.write("info:" + e);
                logger.error("\n" + "程序运行产生异常==================>>>:::",e);
            }
            logger.error("\n" + "<=======================================>PRINT ACTIOIN_ERRO_INFO BEGIN <=======================================>"+"\n");
        } catch (IOException e1) {
            e1.printStackTrace();
            logger.error("异常拦截发生错误======》》》：" + e1);
        }catch (RuntimeException e1) {
            e1.printStackTrace();
            logger.error("异常拦截发生错误======》》》：" + e1);
        }finally{
            if(out!=null){
                out.close();
            }
        }
    }

    public void writeTopoLog(Exception e){
        try {
            logger.error("\n" + "<=======================================>PRINT TOPO_ERRO_INFO BEGIN <=======================================>"+"\n");
            if(e.getCause()!=null && (e.getCause()) instanceof CustomException){
                //将异常信息写入日志
                logger.error("\n" + "拓扑业务运行产生异常==================>>>:::"+((CustomException)e.getCause()).getErrorInfo(),e);
            }else{
                logger.error("\n" + "程序运行产生异常==================>>>:::",e);
            }
            logger.error("\n" + "<=======================================>PRINT TOPO_ERRO_INFO END  <=======================================>" + "\n");
        } catch (Exception e1) {
            logger.error("异常拦截发生错误======》》》：" + e1);
        }
    }

}
